const LOGIN_USER_KEY = "LOGIN_USER_KEY";
const USER_MENU_KEY = "USER_MENU_KEY";
const USER_TOKEN = "USER_TOKEN";
import store from "@/store";

export default {
    setToken(token) {
        sessionStorage.setItem(USER_TOKEN, token);
    },
    getToken() {
        return sessionStorage.getItem(USER_TOKEN);
    },
    setLoginUser(loginUser) {
        sessionStorage.setItem(LOGIN_USER_KEY, JSON.stringify(loginUser));
    },
    getLoginUser() {
        let loginUser = sessionStorage.getItem(LOGIN_USER_KEY);
        if (loginUser) {
            loginUser = JSON.parse(loginUser);
        } else {
            loginUser = {}
        }
        return loginUser;
    },
    isLogin() {
        let loginUser = sessionStorage.getItem(LOGIN_USER_KEY);
        if (loginUser) {
            return true;
        } else {
            return false;
        }
    },
    logout() {
        sessionStorage.removeItem(LOGIN_USER_KEY);
        sessionStorage.removeItem(USER_MENU_KEY);
        store.commit("menu/CLEAR_MENU");
    },
    setUserMenu(userMenu) {
        sessionStorage.setItem(USER_MENU_KEY, JSON.stringify(userMenu));
    },
    getUserMenu() {
        let userMenu = sessionStorage.getItem(USER_MENU_KEY);
        if (userMenu) {
            userMenu = JSON.parse(userMenu);
        } else {
            userMenu = []
        }
        return userMenu;
    },
    loadRoute(ownMenus, router, routes) {
        let layout = {
            path: "/layout",
            name: "layout",
            component: () => import("@/views/Layout"),
            children: [
                {
                    path: "main",
                    component: () => import("@/views/Main"),
                    meta: {
                        title: '首页'
                    }
                }
            ]
        }
        for (let first of ownMenus) {
            for (let second of first.children) {
                let url = second.url;
                let component = second.component;
                let name = second.name;
                layout.children.push({
                    path: url,
                    component: () => import("@/views" + component),
                    meta: {
                        title: name,
                        param1: second.param1
                    }
                });
            }
        }
        if (routes) {
            //路由配置加载
            routes.push(layout)
        } else {
            //登录画面加载
            //将配置配置添加到路由的集合中
            router.options.routes.push(layout);
            //将新添加的路由配置应用到路由中
            router.addRoute(layout);
        }
        
        
        
    }


}